Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
deep-for-each
Advanced tools
The deep-for-each npm package allows you to iterate over all the properties of an object or elements of an array, including nested ones. This is particularly useful for performing operations on deeply nested structures without having to manually traverse each level.
Iterate over all properties of a nested object
This feature allows you to iterate over all properties of a nested object, including those within nested objects and arrays. The callback function provides the value, key, subject, and path of each property.
const deepForEach = require('deep-for-each');
const obj = {
a: 1,
b: { c: 2, d: { e: 3 } },
f: [4, 5, { g: 6 }]
};
deepForEach(obj, (value, key, subject, path) => {
console.log(`Key: ${key}, Value: ${value}, Path: ${path}`);
});
Modify properties of a nested object
This feature allows you to modify the properties of a nested object. In this example, all numeric values are doubled.
const deepForEach = require('deep-for-each');
const obj = {
a: 1,
b: { c: 2, d: { e: 3 } },
f: [4, 5, { g: 6 }]
};
deepForEach(obj, (value, key, subject) => {
if (typeof value === 'number') {
subject[key] = value * 2;
}
});
console.log(obj);
Lodash is a popular utility library that provides a wide range of functions for manipulating arrays, objects, and other data structures. It includes a `_.forEach` method that can be used to iterate over properties of an object or elements of an array, but it does not natively support deep iteration. However, you can achieve similar functionality using `_.cloneDeepWith` or custom recursive functions.
Deepdash is an extension of Lodash that adds deep iteration capabilities. It provides methods like `deepForEach`, `deepMap`, and `deepFilter` that allow you to work with nested structures more easily. It is a good alternative if you are already using Lodash and need deep iteration functionality.
The traverse package provides a set of functions for walking and transforming objects. It includes methods like `forEach`, `map`, and `reduce` that work on deeply nested structures. It is a lightweight alternative to deep-for-each with a focus on object traversal and transformation.
Recursively iterates over collections (arrays and objects). The iteration is made using a deep-first algorithm.
$ npm install deep-for-each
- NPM
$ bower install deep-for-each
- bower
The browser file is named index.umd.js
which supports CommonJS, AMD and globals (deepForEach
).
If you want to run this module on old browsers, you must include es5-shim.
The example bellow is based on nodejs
.
var deepForEach = require('deep-for-each');
deepForEach({
prop1: 'foo',
prop2: ['foo', 'bar'],
prop3: ['foo', 'foo'],
prop4: {
prop5: 'foo',
prop6: 'bar'
}
}, function (value, prop, subject, path) {
// prop is an array index or an object key
// subject is either an array or an object
// path is the iteration path, e.g.: 'prop2[0]' and 'prop4.prop5'
console.log(path + ':', value);
});
Running the example above will print:
prop1: foo
prop2: [ 'foo', 'bar' ]
prop2[0]: foo
prop2[1]: bar
prop3: [ 'foo', 'foo' ]
prop3[0]: foo
prop3[1]: foo
prop4: { prop5: 'foo', prop6: 'bar' }
prop4.prop5: foo
prop4.prop6: bar
$ npm test
Released under the MIT License.
FAQs
Recursively iterates over arrays and objects
The npm package deep-for-each receives a total of 146,445 weekly downloads. As such, deep-for-each popularity was classified as popular.
We found that deep-for-each demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.